package com.hunttown.common.dao.datasource.config;

import java.util.Iterator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

public class SqlMapClientBeanDefinitionParser implements BeanDefinitionParser {
    private static final Logger logger = LoggerFactory.getLogger(SqlMapClientBeanDefinitionParser.class);
    private final Class SQL_MAP_CLIENT_FACTORY_BEAN = SqlMapClientFactoryBean.class;

    public SqlMapClientBeanDefinitionParser() {
    }

    public BeanDefinition parse(Element element, ParserContext parserContext) {
        logger.debug("ibatis-config : ==========start all ibatis config==========");
        List<Element> sqlMapClientElements = DomUtils.getChildElementsByTagName(element, "sql-map-client");
        this.parseSqlMapClients(sqlMapClientElements, parserContext);
        logger.debug("ibatis-config : ==========end all ibatis config config==========");
        return null;
    }

    private void parseSqlMapClients(List<Element> sqlMapClientElements, ParserContext parserContext) {
        Iterator i$ = sqlMapClientElements.iterator();

        while(i$.hasNext()) {
            Element sqlMapClientElement = (Element)i$.next();
            this.parseSqlMapClient(sqlMapClientElement, parserContext);
        }

    }

    private void parseSqlMapClient(Element sqlMapClientElement, ParserContext parserContext) {
        String beanName = sqlMapClientElement.getAttribute("id");
        String dataSourceBeanName = sqlMapClientElement.getAttribute("datasource-ref");
        String configLocation = sqlMapClientElement.getAttribute("config-location");
        logger.debug("sql-map-client config : id : {}, datasource-ref : {}, config-location : {}", new Object[]{beanName, dataSourceBeanName, configLocation});
        if (!parserContext.getRegistry().containsBeanDefinition(dataSourceBeanName)) {
            throw new IllegalArgumentException("sql-map-client config : [" + beanName + "] sql-map-client reference an not exists [" + dataSourceBeanName + "] datasource");
        } else {
            RootBeanDefinition sqlMapClientDefinition = new RootBeanDefinition(this.SQL_MAP_CLIENT_FACTORY_BEAN);
            MutablePropertyValues propertyValues = sqlMapClientDefinition.getPropertyValues();
            propertyValues.add("dataSource", new RuntimeBeanReference(dataSourceBeanName));
            propertyValues.add("configLocation", configLocation);
            parserContext.getRegistry().registerBeanDefinition(beanName, sqlMapClientDefinition);
        }
    }
}